package com.syh.jn.scm.domain.upstream.entity.finance;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.validator.ValidationUtils;
import com.syh.core.ddd.domain.AbstractSimpleAggregate;
import com.syh.jn.scm.dto.support.FilesDto;
import com.syh.jn.scm.dto.upstream.finance.UpIncomeBillSto;
import com.syh.jn.scm.exception.BusinessException;
import com.syh.jn.scm.service.api.upstream.finance.enums.UpIncomeBillEnum;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;

/**
 * 上游收入单
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class UpIncomeBill extends AbstractSimpleAggregate<Long> {
  /**
   * 收入单编号
   */
  private String code;
  /**
   * 客户id
   */
  private Long customerId;
  /**
   * 收入单状态:0待确认, 1已确认, 3已生成账单, 3已取消
   */
  private Short state;
  /**
   * 对方确认生效状态：0未生效, 1已生效
   */
  private Short confirmState;
  /**
   * 录单人id
   */
  private Long brokerageId;
  /**
   * 归属id
   */
  private Long businessEntityId;
  /**
   * 录单日期
   */
  private Long inputTimestamp;
  /**
   * 收入金额
   */
  private BigDecimal price;
  /**
   * 备注
   */
  private String remark;
  /**
   * 对账单id
   */
  private Long statementOfAccountId;
  /**
   * 单据图片
   */
  private String secondSrcImages;

  public UpIncomeBill(){}

  public UpIncomeBill(UpIncomeBillSto sto) {
    ExtAssert.notNull(sto,"对象为空");
    ValidationUtils.validate(sto);
    ExtBeanUtils.copyProperties(sto,this);
  }

  /**
   * 确认收入单生效
   */
  public void confirmState(){
    if(!this.confirmState.equals(UpIncomeBillEnum.OTHERPARTY_STAYCONFIRM.getStatus())){
      throw new BusinessException("收入单不是未生效状态不可以确认生效");
    }
    this.confirmState = UpIncomeBillEnum.OTHERPARTY_CONFIRM.getStatus();
  }

  /**
   * 取消确认生效
   */
  public void cancelConfirm(){
    if(!this.confirmState.equals(UpIncomeBillEnum.OTHERPARTY_CONFIRM.getStatus())){
      throw new BusinessException("收入单不是已生效状态不可以取消确认生效");
    }
    if (this.state.equals(UpIncomeBillEnum.STATEMENT.getStatus())) {
      throw new BusinessException("收入单已生产对账单，无法取消确认！!");
    }
    this.confirmState = UpIncomeBillEnum.OTHERPARTY_STAYCONFIRM.getStatus();
  }

  /**
   * 生成对账单
   */
  public void generating(Long statementOfAccountId){
    if (this.state.equals(UpIncomeBillEnum.STATEMENT.getStatus())) {
      throw new BusinessException("收入单已生成对账单!");
    }
    this.statementOfAccountId = statementOfAccountId;
    this.state = UpIncomeBillEnum.STATEMENT.getStatus();
  }

  /**
   * 取消生成对账单
   */
  public void cancelGenerating(){
    if (!this.state.equals(UpIncomeBillEnum.STATEMENT.getStatus())) {
      throw new BusinessException("收入单已取消对账单!");
    }
    this.statementOfAccountId = null;
    this.state = UpIncomeBillEnum.CONFIRM.getStatus();
  }

  /**
   * 上传图片
   *
   * @param dto
   */
  public void upload(FilesDto dto) {
    this.secondSrcImages = dto.getFiles();
  }
}
